<%--
  Created by IntelliJ IDEA.
  User: 廉壮
  Date: 2023/5/17
  Time: 9:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>
<head>
    <meta charset="UTF-8">
    <title>发表朋友圈</title>
    <script src="${ctx}/static/js/jquery.js"></script>
    <style>
        /* 样式表 */
        body {
            font-family: Arial, sans-serif;
        }

        h1 {
            text-align: center;
        }

        form {
            width: 50%;
            margin: 0 auto;
        }

        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        input[type="text"], textarea {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
            border: none;
            box-shadow: 1px 1px 5px #ccc;
        }

        textarea {
            height: 100px;
        }

        input[type="submit"] {
            display: block;
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        input[type="submit"]:hover {
            background-color: #3e8e41;
        }

        .file-upload {
            display: block;
            width: 100%;
            margin-bottom: 10px;
        }

        .file-upload label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        .file-upload input[type="file"] {
            display: block;
            width: 100%;
            padding: 10px;
            border-radius: 5px;
            border: none;
            box-shadow: 1px 1px 5px #ccc;
        }

        .img {
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
<h1>发表朋友圈</h1>
<form action="${ctx}/topic/add">
    <label for="content">内容：</label>
    <textarea id="content" name="content" placeholder="请输入您要发表的内容"></textarea><br>
    <input id="image" name="image" type="text" value="" style="display: none">
    <div class="file-upload">
        <label for="image">图片：</label>
        <input type="file" id="file" name="file" class="btn-upload">
    </div>
    <div class="img">
        <img id="img" src="" alt="" width="100px"/>
    </div>
    <input type="submit" class="bt_save" value="发表">
</form>
</body>
<script>
    $(function () {
        $("#file").click(function () {
            $("#file").change(function () {
                $.ajax({
                    url: "${ctx}/common/uploadOne",
                    type: "POST",
                    data: new FormData($("form")[0]),
                    processData: false,		//是否将数据序列化成查询字符串，默认为true
                    contentType: false,		//设置为false，否则默认是表单内容
                    success: function (e) {
                        console.log(e);
                        if (e == "err") {
                            alert("上传失败");
                        } else {
                            $("#img").attr("src", "${ctx}/common/getImage?image=" + e);
                            $("#image").attr("value", e)
                        }
                    }
                })
            })
        })

        $(".bt_save").on("click", function () {
            $.get('${ctx}/topic/add', $("form").serialize(), function (e) {
                if (e == "ok") {
                    location.href = '${ctx}/topic/list';
                } else {
                    alert("添加失败");
                }
            })
            return false;
        })
    })
</script>
</html>
